<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        name="mobile"
        v-model="user.mobile"
        :rules="userFormRules.mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
      >
        <template #left-icon>
          <i class="iconfont icon-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        :rules="userFormRules.code"
        placeholder="请输入密码"
        type="number"
        maxlength="6"
      >
        <template #left-icon>
          <i class="iconfont icon-yanzhengma"></i>
        </template>
        <template #button v-if="isCountDownShow">
          <van-count-down
            @finish="isCountDownShow = false"
            format="ss 秒后重新发送"
            :time="1000 * 30"
          />
        </template>
        <template #button v-else>
          <van-button
            native-type="button"
            class="send-sms-btn"
            round
            size="small"
            type="default"
            @click="sendCode"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          round
          block
          type="info"
          native-type="submit"
          >登 录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user.js'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '18888888888',
        code: '246810'
      },
      // 验证规则
      userFormRules: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      // 验证码倒计时开关
      isCountDownShow: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 发送验证码
    async sendCode () {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('手机号没填，先填对手机号')
      }
      this.isCountDownShow = true
      try {
        await getSmsCode(this.user.mobile)
        this.$toast.success('发送成功')
      } catch (err) {
        this.$toast.fail(err?.response?.data?.message || '请求超时，请重试')
      }
    },
    async onSubmit () {
      this.$toast.loading({
        duration: 0, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '登录中...' // 提示消息
      })
      try {
        const res = await login(this.user)
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } catch (err) {
        this.$toast.fail(err.response.data.message)
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .van-form {
    margin-top: 25px;
  }
  .iconfont {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 162px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
      height: 80px;
    }
  }
}
</style>
